#region File Description
//-----------------------------------------------------------------------------
// PauseMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Net;
#endregion

namespace TankWars
{
    /// <summary>
    /// The pause menu comes up over the top of the game,
    /// giving the player options to resume or quit.
    /// </summary>
    class PauseMenuScreen : MenuScreen
    {
        #region Fields and Properties

        // Determines whether network session must be ended.
        NetworkSession networkSession;

        MenuEntry resumeGameMenuEntry;
        MenuEntry fullScreenMenuEntry;
        MenuEntry controllerHelpMenuEntry;

        internal MenuEntry FullScreenMenuEntry
        {
            get { return fullScreenMenuEntry; }
            set { fullScreenMenuEntry = value; }
        }        

        #endregion

        #region Initialization


        // Overloaded constructor
        public PauseMenuScreen(NetworkSession theSession) : base("Paused")
        {
            // Point to session
            this.networkSession = theSession;

            // Then initialize...
            // Flag that there is no need for the game to transition
            // off when the pause menu is on top of it.
            IsPopup = true;

            // Create our static menu entries.
            resumeGameMenuEntry = new MenuEntry("Resume Game");
            fullScreenMenuEntry = new MenuEntry(string.Empty);
            controllerHelpMenuEntry = new MenuEntry("Controls");
            // Hook up static menu event handlers.
            resumeGameMenuEntry.Selected += OnCancel;
            fullScreenMenuEntry.Selected += ResolutionMenuEntrySelected;
            controllerHelpMenuEntry.Selected += ControllerHelpMenuEntrySelected;
            // Add static entries to the menu.
            MenuEntries.Add(resumeGameMenuEntry);
            MenuEntries.Add(fullScreenMenuEntry);
            MenuEntries.Add(controllerHelpMenuEntry);

            if (this.networkSession == null)
            {
                // If this is a single player game, add the Quit menu entry.
                MenuEntry quitGameMenuEntry = new MenuEntry("Quit Game");
                quitGameMenuEntry.Selected += QuitGameMenuEntrySelected;
                MenuEntries.Add(quitGameMenuEntry);
            }
            else
            {
                // If we are hosting a network game, add the Return to Lobby menu entry.
                if (this.networkSession.IsHost)
                {
                    MenuEntry lobbyMenuEntry = new MenuEntry("Return to the Lobby");
                    lobbyMenuEntry.Selected += ReturnToLobbyMenuEntrySelected;
                    MenuEntries.Add(lobbyMenuEntry);
                }

                // Add the End/Leave Session menu entry.
                string leaveEntryText = this.networkSession.IsHost ? "End Session" : "Leave Session";

                MenuEntry leaveSessionMenuEntry = new MenuEntry(leaveEntryText);
                leaveSessionMenuEntry.Selected += LeaveSessionMenuEntrySelected;
                MenuEntries.Add(leaveSessionMenuEntry);
            }

            // Start with a certain type of resolution
            SetMenuEntryText();           
        }
        

        /// <summary>
        /// Fills in the latest values for the pause screen menu text.
        /// </summary>
        void SetMenuEntryText()
        {            
            FullScreenMenuEntry.Text = "Window Mode: " + (FullScreenOn ? "Full Screen" : "Window");
        }


        #endregion

        #region Handle Input


        /// <summary>
        /// Event handler for when the Quit Game menu entry is selected.
        /// </summary>
        void QuitGameMenuEntrySelected(object sender, EventArgs e)
        {
            const string message = "Are you sure you want to quit this game?";

            MessageBoxScreen confirmQuitMessageBox = new MessageBoxScreen(message);

            confirmQuitMessageBox.Accepted += ConfirmQuitMessageBoxAccepted;

            ScreenManager.AddScreen(confirmQuitMessageBox);
        }


        /// <summary>
        /// Event handler for when the Resolution menu entry is selected.
        /// </summary>
        void ResolutionMenuEntrySelected(object sender, EventArgs e)
        {
            ScreenManager.TankWarsGame.Graphics.ToggleFullScreen();

            FullScreenOn = !FullScreenOn;   // toggle full-screen

            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for selection of the controller help menu entry
        /// </summary>
        void ControllerHelpMenuEntrySelected(object sender, EventArgs e)
        {
            ScreenManager.AddScreen(new PictureScreen());
        }

        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to quit" message box. This uses the loading screen to
        /// transition from the game back to the main menu screen.
        /// </summary>
        void ConfirmQuitMessageBoxAccepted(object sender, EventArgs e)
        {
            // Dispose of the local gameplay elements and return to the main menu
            LoadingScreen.Load(ScreenManager, false, new BackgroundScreen(), new MainMenuScreen());
        }

        /// <summary>
        /// Event handler for when the Return to Lobby menu entry is selected.
        /// </summary>
        void ReturnToLobbyMenuEntrySelected(object sender, EventArgs e)
        {
            if (this.networkSession.SessionState == NetworkSessionState.Playing)
            {
                this.networkSession.EndGame();
            }
        }

        /// <summary>
        /// Event handler for when the End/Leave Session menu entry is selected.
        /// </summary>
        void LeaveSessionMenuEntrySelected(object sender, EventArgs e)
        {
            NetworkSessionComponent.LeaveSession(ScreenManager);
        }


        #endregion

        #region Draw


        /// <summary>
        /// Draws the pause menu screen. This darkens down the gameplay screen
        /// that is underneath us, and then chains to the base MenuScreen.Draw.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 1 / 2);

            base.Draw(gameTime);
        }


        #endregion
    }
}
